
#import "FindTableViewController.h"
#import "FindSortCell.h"
#import "FindTitleCell.h"
#import "FindContentCell.h"
#import "LoginViewController.h"
#import "FindRadioCell.h"
#import "VerticalButton.h"
#import "LoadWebViewController.h"
#import "XCMNearbyTableViewController.h"
#import "GroupViewController.h"
#import "CrossTableViewController.h"

@interface FindTableViewController ()
/** 存放每个分区行数的数组 */
@property (nonatomic, strong) NSMutableArray *rowCountArr;
/** 存放分区第一行的图片数组 */
@property (nonatomic, strong) NSMutableArray *lineOnetitleIconImageArr;
/** 存放分区第一行的主标题数组 */
@property (nonatomic, strong) NSMutableArray *lineOnetitleArr;
/** 存放分区第一行的副标题数组 */
@property (nonatomic, strong) NSMutableArray *lineOneSubTitleArr;
/** 游戏模块图片数组 */
@property (nonatomic, strong) NSMutableArray *gameIconImageArr;
/** 游戏模块主标题数组 */
@property (nonatomic, strong) NSMutableArray *gameTitleArr;
/** 游戏模块副标题数组 */
@property (nonatomic, strong) NSMutableArray *gameSubTitleArr;

@end

@implementation FindTableViewController

static NSString * const sortID = @"findSort";
static NSString * const titleID = @"findTitle";
static NSString * const contentID = @"findContent";
static NSString * const radioID = @"findRadio";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 标题
    self.navigationItem.title = @"发现";
    
    // 去除分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FindSortCell class]) bundle:nil] forCellReuseIdentifier:sortID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FindTitleCell class]) bundle:nil] forCellReuseIdentifier:titleID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FindContentCell class]) bundle:nil] forCellReuseIdentifier:contentID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FindRadioCell class]) bundle:nil] forCellReuseIdentifier:radioID];
    
    // 去除默认表尾高度
    self.tableView.tableFooterView = [[UIView alloc] init];
}

#pragma mark - 穿越
- (void)crossAction
{
    CrossTableViewController *crossTableVC = [[CrossTableViewController alloc] init];
    [crossTableVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:crossTableVC animated:YES];
}

#pragma mark - 附近
- (void)nearbyAction
{
    XCMNearbyTableViewController *nearbyVC = [[XCMNearbyTableViewController alloc] init];
    [nearbyVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:nearbyVC animated:YES];
}

#pragma mark - 群
- (void)groupAction
{
    GroupViewController *groupVC = [[GroupViewController alloc] init];
    [groupVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:groupVC animated:YES];
}

#pragma mark - 小鸡
- (void)smallAction
{
    LoadWebViewController *loadWebVC = [[LoadWebViewController alloc] init];
    [loadWebVC setHidesBottomBarWhenPushed:YES];
    loadWebVC.navTitle = @"糗事百科";
    loadWebVC.webString = @"http://www.qiushibaike.com/topic";
    [self.navigationController pushViewController:loadWebVC animated:YES];
}

#pragma mark - 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.rowCountArr.count;
}

#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = [self.rowCountArr[section] integerValue];
    return count;
}

#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FindSortCell *cell = [tableView dequeueReusableCellWithIdentifier:sortID];
        [cell.crossBtn addTarget:self action:@selector(crossAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.nearbyBtn addTarget:self action:@selector(nearbyAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.groupBtn addTarget:self action:@selector(groupAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.smallChickenBtn addTarget:self action:@selector(smallAction) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section != 0 && indexPath.row == 0) {
        FindTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:titleID];
        // 设置图片
        cell.iconImageView.image = [UIImage imageNamed:self.lineOnetitleIconImageArr[indexPath.section - 1]];
        // 设置主标题
        cell.titleLabel.text = self.lineOnetitleArr[indexPath.section - 1];
        // 设置副标题
        cell.subTitleLabel.text = self.lineOneSubTitleArr[indexPath.section - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        FindRadioCell *cell = [tableView dequeueReusableCellWithIdentifier:radioID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        FindContentCell *cell = [tableView dequeueReusableCellWithIdentifier:contentID];
        // 设置游戏图片
        cell.iconImageView.image = [UIImage imageNamed:self.gameIconImageArr[indexPath.row - 1]];
        // 设置游戏主标题
        cell.titleLabel.text = self.gameTitleArr[indexPath.row - 1];
        // 设置游戏副标题
        cell.subTitleLabel.text = self.gameSubTitleArr[indexPath.row - 1];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100;
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        return 100;
    } else {
        return 70;
    }
}

#pragma mark - 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

#pragma mark - 区尾高度（去除默认表尾高度）
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

#pragma mark - 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoadWebViewController *loadWebVC = [[LoadWebViewController alloc] init];
    [loadWebVC setHidesBottomBarWhenPushed:YES];
    
    if (indexPath.section == 1 && indexPath.row == 0) { // 游戏中心
        loadWebVC.navTitle = @"糗百游戏中心";
        loadWebVC.webString = [NSString stringWithFormat:@"http://api.qiushibaike.com/verify/code?url=http://gc.hgame.com/app/qsbk/transfer?from=qbios&token=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]];
        [self.navigationController pushViewController:loadWebVC animated:YES];
    } else if (indexPath.section == 1 && indexPath.row == 1) { // 时光德州
        loadWebVC.navTitle = self.gameTitleArr[0];
        loadWebVC.webString = [NSString stringWithFormat:@"http://api.qiushibaike.com/verify/code?url=http://gc.hgame.com/app/qsbk/transfer/gameid/100265?from=qbios&token=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]];
        [self.navigationController pushViewController:loadWebVC animated:YES];
    } else if (indexPath.section == 1 && indexPath.row == 2) { // 传奇世界
        loadWebVC.navTitle = self.gameTitleArr[1];
        loadWebVC.webString = [NSString stringWithFormat:@"http://api.qiushibaike.com/verify/code?url=http://gc.hgame.com/app/qsbk/transfer/gameid/100220?from=qbios&token=%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]];
        [self.navigationController pushViewController:loadWebVC animated:YES];
    } else if (indexPath.section == 2) { // 小鸡炖蘑菇
        [self smallAction];
    }
}

#pragma mark - 跳转登录界面
- (void)skipToLoginVC
{
    // 跳转登录界面
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
}

#pragma mark - 懒加载
- (NSMutableArray *)rowCountArr
{
    if (!_rowCountArr) {
        _rowCountArr = [@[@"1", @"3", @"2"] mutableCopy];
    }
    return _rowCountArr;
}

- (NSMutableArray *)lineOnetitleIconImageArr
{
    if (!_lineOnetitleIconImageArr) {
        _lineOnetitleIconImageArr = [@[@"game", @"jitou"] mutableCopy];
    }
    return _lineOnetitleIconImageArr;
}

- (NSMutableArray *)lineOnetitleArr
{
    if (!_lineOnetitleArr) {
        _lineOnetitleArr = [@[@"游戏", @"小鸡炖蘑菇"] mutableCopy];
    }
    return _lineOnetitleArr;
}

- (NSMutableArray *)lineOneSubTitleArr
{
    if (!_lineOneSubTitleArr) {
        _lineOneSubTitleArr = [@[@"更多精选好玩儿游戏", @"糗事百科出品大综艺"] mutableCopy];
    }
    return _lineOneSubTitleArr;
}

- (NSMutableArray *)gameIconImageArr
{
    if (!_gameIconImageArr) {
        _gameIconImageArr = [@[@"shiguangdezhou", @"chuanqi"] mutableCopy];
    }
    return _gameIconImageArr;
}

- (NSMutableArray *)gameTitleArr
{
    if (!_gameTitleArr) {
        _gameTitleArr = [@[@"时光德州", @"传奇世界"] mutableCopy];
    }
    return _gameTitleArr;
}

- (NSMutableArray *)gameSubTitleArr
{
    if (!_gameSubTitleArr) {
        _gameSubTitleArr = [@[@"玩时光德州，赢幸运大奖", @"传奇首创，再现巅峰，大型智能网络游戏"] mutableCopy];
    }
    return _gameSubTitleArr;
}
@end
